package com.naiterui.ehp.bp.utils.consult;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 视频问诊工具类
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021-02-26 11:19
 **/
public class VideoConsultUtil {

    public static final String DOCTOR_PREFIX = "doctor-";
    public static final String DOCTOR_PC_PREFIX = "doctor-pc-";
    public static final String PATIENT_PREFIX = "patient-";

    /**
     * 获取房间号
     *
     * @return 房间号
     */
    public static Integer getRoomId() {
        long nextId = IdUtil.getSnowflake(0, 0).nextId();
        long roomId = nextId + (System.currentTimeMillis() + RandomUtil.randomInt(5));
        return Long.valueOf(roomId / 10000000000L).intValue();
    }

    /**
     * 判断是否是APP端医生
     *
     * @param userId 用户id
     * @return 结果
     */
    public static boolean containsDoctor(String userId) {
        return StrUtil.contains(userId, DOCTOR_PREFIX);
    }

    /**
     * 验证是否是PC端医生
     *
     * @param userId 用户id
     * @return 结果
     */
    public static boolean containsDoctorPc(String userId) {
        return StrUtil.contains(userId, DOCTOR_PC_PREFIX);
    }

    /**
     * 获取id
     * @param userId
     * @param prefix
     * @return
     */
    public static Long getId(String userId, String prefix){
        if (userId == null || !userId.contains(prefix)) {
            return null;
        }
        return Long.valueOf(userId.replace(prefix, ""));
    }

    /**
     * 获取id
     * @param userId
     * @return
     */
    public static Long getId(String userId){
        if (userId == null) {
            return null;
        }
        String id;
        if (containsDoctorPc(userId)) {
            id = userId.replace(DOCTOR_PC_PREFIX, "");
        } else if (containsDoctor(userId)) {
            id = userId.replace(DOCTOR_PREFIX, "");
        } else {
            id = userId.replace(PATIENT_PREFIX, "");
        }
        return Long.valueOf(id);
    }
}
